<!doctype html>
<html class="no-js">

<head>
    <title>修改信息</title>
    <#include "/fd/components/meta.html" />
    <#include "/fd/components/stylesheets.html" />
    <link rel="stylesheet" type="text/css" href="/fd/stylesheets/updateMasterInfo.css?v=3">
</head>

<body>
    <div id="app">
        <div class="container">
            <div class="field">
                <label>头像</label>
                <!-- <div class="input">
                    <input type="text" placeholder="请输入" v-model="url" />
                </div> -->
                <div class="cover" v-if="url == ''">
                    <div class="cover-upload" @click="selectCover">
                        <i class="am-icon-plus"></i>
                    </div>
                </div>
                <div class="cover" v-else>
                    <div class="cover-img fillimg">
                        <img :src="url" @click="selectCover" />
                        <button type="button" class="am-close" @click="deleteCover">&times;</button>
                    </div>
                </div>
            </div>
            <div class="field">
                <label>名称</label>
                <div class="input">
                    <input type="text" placeholder="请输入" v-model="name" />
                </div>
            </div>
            <div class="field">
                <label>加入时间（年）</label>
                <div class="input">
                    <input type="text" placeholder="请输入" v-model="joinYear" />
                </div>
            </div>
            <div class="field">
                <label>描述</label>
                <div class="input">
                    <textarea placeholder="请输入" v-model="desc" rows="3"></textarea>
                </div>
            </div>
            <div class="field">
                <label>简介</label>
                <div class="input">
                    <textarea placeholder="请输入" v-model="intro" rows="3"></textarea>
                </div>
            </div>
            <div class="field">
                <label>方向</label>
                <div class="input">
                    <input type="checkbox" v-model="good" value="1" />&nbsp;英文&nbsp;
                    <input type="checkbox" v-model="good" value="2" />&nbsp;数学&nbsp;
                    <input type="checkbox" v-model="good" value="3" />&nbsp;语文&nbsp;
                </div>
            </div>
            <div class="field">
                <label>价格</label>
                <div class="input">
                    <input type="number" placeholder="请输入" v-model="price" />
                </div>
            </div>
            <div class="field">
                <label>原价格</label>
                <div class="input">
                    <input type="number" placeholder="请输入" v-model="oldPrice" />
                </div>
            </div>
            <div class="submit">
                <button class="am-btn am-btn-default" @click='onSubmit'>提交</button>
            </div>
        </div>
    </div>
    <#include "/fd/components/scripts.html" />
    <#include "/fd/components/http.html" />
    <#include "/fd/components/wxConfig.html" />
    <script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data: {
            imgServerId: '',
            url: "${info.logo!}",
            name: "${info.realName!}",
            desc: "${info.desc!}",
            joinYear: "${info.joinYears!}",
            good: [],
            price: "${(info.price*0.01)!}",
            oldPrice: "${(info.oldPrice*0.01)!}",
            intro: "${info.intro!}",
        },
        mounted: function() {
            var goodAt = "${info.goodAt!''}";
            if (goodAt != "") {
                this.good = goodAt.split(";");
            }
        },
        methods: {
            selectCover() {
                var self = this;
                wx.chooseImage({
                    count: 1, // 图片数限制
                    sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
                    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                    success: function(res) {
                        var localId = res.localIds[0]; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                        if (isAndroid_ios()) {
                            self.url = localId;
                        } else {
                            wx.getLocalImgData({
                                localId: localId, // 图片的localID
                                success: function(res) {
                                    self.url = res.localData; // localData是图片的base64数据，可以用img标签显示
                                }
                            });
                        }
                        wx.uploadImage({
                            localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得
                            isShowProgressTips: 1, // 默认为1，显示进度提示
                            success: function(res) {
                                var serverId = res.serverId; // 返回图片的服务器端ID
                                self.imgServerId = serverId;
                            }
                        });
                        setTimeout("adjustImgs()", 500);
                    }
                });
            },
            deleteCover() {
                this.url = '';
                this.imgServerId = '';
            },
            onSubmit: function() {
                var goodAt = "";
                for (var index = 0; index < this.good.length; index ++) {
                    if (this.good[index] != "") {
                        goodAt += this.good[index] + ";";
                    }
                }
                var param = {
                    name: this.name,
                    desc: this.desc,
                    joinYear: this.joinYear,
                    good: goodAt,
                    price: this.price,
                    oldPrice: this.oldPrice,
                    intro: this.intro,
                }
                if (this.imgServerId != '') {
                    param['imgServerId'] = this.imgServerId;
                } else {
                    param['url'] = this.url;
                }
                var success = function(data) {
                    layer.open({
                        content: '修改成功',
                        btn: '确定',
                        end: function() {
                            window.location.href = '/fd/master/';
                        }
                    });
                }
                var fail = function() {}
                request('/fd/master/updateMasterInfo', param, success, fail)
            }
        }
    })

    function isAndroid_ios() {
        // true: android; false: ios
        var u = navigator.userAgent,
            app = navigator.appVersion;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        return isAndroid == true ? true : false;
    }
    </script>
</body>

</html>